//#define BOOST_MP_STANDALONE

#include "boost/multiprecision/cpp_int.hpp"
#include "xprime.h"
#include "xtest.h"
#include "xutil.h"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <random>

//import std;

namespace mpn = boost::multiprecision;

using namespace std;

//错误的，狗日的AI 乱传代码，瞎几把 乱答案
int sar_adc(float input_voltage, float v_ref, int bits)
{
    int result = 0;
    float step_size = v_ref / pow(2, bits); // 计算步长

    for (int i = bits - 1; i >= 0; --i) {
        result <<= 1;                                   // 左移一位
        float mid_voltage = ((result | 1) * step_size); // 计算中间电压

        if (input_voltage >= mid_voltage) {
            result |= 1; // 设置最低位为 1
        }
    }

    return result;
}

int main3()
{
    float d = 5.0 / pow(2, 10);
    int a = sar_adc(1 * d, 5, 10);
    cout << 15 * d << endl;
    cout << a << endl;

    cout << bitset<10>(a) << endl;
    cout << a * 1.0 / (pow(2, 10) - 1) * 5 << endl;

    return 0;
}
